package com.apps.ftapp.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.apps.ftapp.R;
import com.apps.ftapp.util.Async;
import com.apps.ftapp.util.Constant;
import com.apps.ftapp.util.L;
import com.apps.ftapp.util.Tools;
import com.google.gson.Gson;
import com.lljjcoder.city_20170724.CityPickerView;
import com.lljjcoder.city_20170724.bean.CityBean;
import com.lljjcoder.city_20170724.bean.DistrictBean;
import com.lljjcoder.city_20170724.bean.ProvinceBean;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import cz.msebera.android.httpclient.Header;
import es.dmoral.toasty.Toasty;

public class UserAddressAddActivity extends BaseActivity implements View.OnClickListener {


    @BindView(R.id.nickname)
    EditText nickname;
    @BindView(R.id.mobile)
    EditText mobile;
    @BindView(R.id.city)
    TextView city111;
    @BindView(R.id.address)
    EditText address;
    @BindView(R.id.submit)
    Button submit;

    private String city1;
    private String province1;
    private String district1;

    private Gson gson = new Gson();

    private String addressId;
    private String e_name;
    private String e_mobile;
    private String e_address;
    private String e_city;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_address_add);
        ButterKnife.bind(this);

        setTitle("添加收货地址");

        //新页面接收数据
        Bundle bundle = this.getIntent().getExtras();
        //接收name值
        addressId = bundle.getString("addressId");
        e_name = bundle.getString("name");
        e_mobile = bundle.getString("mobile");
        e_address = bundle.getString("address");
        e_city = bundle.getString("city");

        province1 = bundle.getString("province");
        city1 = bundle.getString("citys");
        district1 = bundle.getString("district");

//        city111 = e_city;
        L.i("main", "=====" + e_city);
//        district1 = e_city;
//        detail_address = e_address;

        city111.setOnClickListener(this);
        submit.setOnClickListener(this);


        mobile.setText(e_mobile);
        if (e_mobile != null) {
            mobile.setSelection(e_mobile.length());
        }


        address.setText(e_address);
        if (e_address != null) {

            address.setSelection(e_address.length());
        }
        city111.setText(e_city);

        nickname.setText(e_name);
        if (e_name != null) {
            nickname.setSelection(e_name.length());
        }
    }


    public void add(String name, String mobile, String address) {
        RequestParams params = new RequestParams();
        params.add("token", Constant.token);
        params.add("userId", Constant.uid);
        params.add("mobile", mobile);
        params.add("user_name", name);
        params.add("province", province1);
        params.add("city", city1);
        params.add("district", district1);
        params.add("detail_address", address);
//        if(!Tools.isNull(addressId).equals(""))
        params.add("address_id", addressId);

        L.i("main", addressId + "::::provice::" + province1 + "::::" + city1 + "::::" + district1 + ":::add:::" + address);


        Async.post("my/address/add", params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                if (statusCode == 200) {
                    L.i("main", new String(responseBody));
                    //请求成功
                    Map jsonBean = gson.fromJson(new String(responseBody), Map.class);
                    L.i("main", jsonBean.toString());
                    if (1 == Tools.isDoubleNull(jsonBean.get("success")) || 1.0 == Tools.isDoubleNull(jsonBean.get("success"))) {
                        Toasty.normal(getApplicationContext(), "操作成功").show();
                        finish();
                    } else {
                        Toasty.normal(getApplicationContext(), Tools.isNull(jsonBean.get("error"))).show();
                    }

                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {

            }
        });
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.submit:

                submit();

                break;

            case R.id.city:

                city();

                break;
        }
    }

    private void submit() {

        String cname = nickname.getText().toString();
        String cmobile = mobile.getText().toString();
        String caddress = address.getText().toString();

        if ("".equals(Tools.isNull(cname))) {

            Toasty.normal(getApplicationContext(), "请添加收货人姓名").show();
            return;
        }
        if ("".equals(Tools.isNull(cmobile))) {

            Toasty.normal(getApplicationContext(), "请添加手机号").show();
            return;
        }
        if ("".equals(Tools.isNull(city1))) {

            Toasty.normal(getApplicationContext(), "请选择省市区").show();
            return;
        }
        if ("".equals(Tools.isNull(caddress))) {

            Toasty.normal(getApplicationContext(), "请添加地址").show();
            return;
        }

        add(cname, cmobile, caddress);


    }

    public void city() {
        CityPickerView cityPicker = new CityPickerView.Builder(UserAddressAddActivity.this)
                .textSize(20)
                .title("地址选择")
                .backgroundPop(0xa0000000)
                .titleBackgroundColor("#999999")
                .titleTextColor("#000000")
                .backgroundPop(0xa0000000)
                .confirTextColor("#000000")
                .cancelTextColor("#000000")
                .province("北京")
                .city("北京")
                .district("朝阳区")
                .textColor(Color.parseColor("#000000"))
                .provinceCyclic(true)
                .cityCyclic(false)
                .districtCyclic(false)
                .visibleItemsCount(7)
                .itemPadding(10)
                .onlyShowProvinceAndCity(false)
                .build();
        cityPicker.show();

        //监听方法，获取选择结果
        cityPicker.setOnCityItemClickListener(new CityPickerView.OnCityItemClickListener() {
            @Override
            public void onSelected(ProvinceBean province, CityBean city, DistrictBean district) {
                //返回结果
                //ProvinceBean 省份信息
                province1 = province.getName();
                //CityBean     城市信息
                city1 = city.getName();
                //DistrictBean
                district1 = district.getName();
                city111.setText(province1 + city1 + district1);
            }

            @Override
            public void onCancel() {

            }
        });
    }


}
